<template>
  <div class="mod-prod">
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :table-loading="dataListLoading"
               :option="tableOption"
               @search-change="searchChange"
               @selection-change="selectionChange"
               @on-load="getDataList">
      <template slot="menuLeft">
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="small"
                   @click.stop="addOrUpdateHandle()">新增</el-button>
      </template>
      <template slot="contact" slot-scope="scope">
        <div>
          <div class="content">姓名: {{scope.row.jobContactName}}</div>
          <div class="content">手机: {{scope.row.jobContactMobile}}</div>
        </div>
      </template>

      <template slot-scope="scope"
                slot="status">
        <el-tag v-if="scope.row.status == 1"
                size="small">上架</el-tag>
        <el-tag v-else
                size="small" type="danger">下架</el-tag>
      </template>

      <template slot-scope="scope"
                slot="menu">
        <el-button type="text"
                   @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
        <el-button type="text"
                   @click="deleteHandle(scope.row.id)">删除</el-button>
        <el-button type="text"
                   @click="offOrOnJob(scope.row)">{{scope.row.status == 0 ? '上架' : '下架'}}</el-button>
<!--        <el-button type="text"-->
<!--                   @click="positionTop(scope.row.id)">置顶</el-button>-->

      </template>
    </avue-crud>
  </div>
</template>

<script>
  import { tableOption } from '@/crud/job/job'
  export default {
    data () {
      return {
        dataForm: {
          name: '',
          factoryName: ''
        },
        dataList: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        dataListLoading: false,
        tableOption: tableOption
      }
    },
    methods: {
      // 获取数据列表
      getDataList (page, params) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/v1/job/list'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize,
                name: params ? params.name : '',
                factoryName:params ? params.factoryName : ''
              }
            )
          )
        }).then(({ data }) => {
          console.log(data)
          this.dataList = data.data.records.map((item)=>{
            item.labelStr = item.labelList.join(',')
            return item
          })
          this.page.total = data.data.total
          this.dataListLoading = false
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.$router.push({
          path: '/jobInfo',
          query: { id: id }
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let param = {
              id: id,
            }
            this.$http({
              url: this.$http.adornUrl(`/v1/job/delete`),
              method: 'post',
              data: this.$http.adornData(param)
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(this.page)
                }
              })
            })
          })
          .catch(() => { })
      },

      // 下架或上架
      offOrOnJob (row) {
        this.$confirm(`确定进行【${row.status == 0 ? '上架' : '下架'}】操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let param = {
            id: row.id,
            status: row.status == 0 ? 1 : 0
          }
          this.$http({
            url: this.$http.adornUrl('/v1/job/enable'),
            method: 'post',
            data: this.$http.adornData(param)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        }).catch(() => { })
      },

      // positionTop (id) {
      //   this.$confirm(`确定进行【置顶】操作?`, '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     this.$http({
      //       url: this.$http.adornUrl('/position/top'),
      //       method: 'post',
      //       data: this.$http.adornData({id: id})
      //     }).then(({ data }) => {
      //       this.$message({
      //         message: '操作成功',
      //         type: 'success',
      //         duration: 1500,
      //         onClose: () => {
      //           this.getDataList(this.page)
      //         }
      //       })
      //     })
      //   })
      // },

      // 条件查询
      searchChange (params, done) {
        done()
        this.getDataList(this.page, params)
      },
      // 多选变化
      selectionChange (val) {
        this.dataListSelections = val
      }
    }
  }
</script>
